package com.lab.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.lab.common.core.domain.model.LoginUser;
import com.lab.system.domain.ExperimentCourse;
import com.lab.system.domain.Lab;
import com.lab.system.domain.Reservations;
import com.lab.system.mapper.ExperimentCourseMapper;
import com.lab.system.mapper.LabMapper;
import com.lab.system.mapper.ReservationsMapper;
import com.lab.system.service.IExperimentCourseService;
import com.lab.system.service.ILabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 实验课程Service业务层处理
 *
 * @author cf
 * @date 2025-02-10
 */
@Service
public class ExperimentCourseServiceImpl implements IExperimentCourseService {
    @Autowired
    private ExperimentCourseMapper experimentCourseMapper;

    @Autowired
    private LabMapper labMapper;

    @Autowired
    private ILabService labService;

    @Autowired
    private ReservationsMapper reservationsMapper;

    /**
     * 查询实验课程
     *
     * @param id 实验课程主键
     * @return 实验课程
     */
    @Override
    public ExperimentCourse selectExperimentCourseById(Long id) {
        return experimentCourseMapper.selectExperimentCourseById(id);
    }

    /**
     * 查询实验课程列表
     *
     * @param experimentCourse 实验课程
     * @return 实验课程
     */
    @Override
    public List<ExperimentCourse> selectExperimentCourseList(ExperimentCourse experimentCourse, LoginUser loginUser) {

        // 数据权限控制
        List<Lab> labs1 = labService.selectLabList(new Lab(),loginUser);
        List<Long> labIds1 =  labs1.stream().map(Lab::getId).collect(Collectors.toList());
        // 查询所有实验室有的课程
        experimentCourse.setLabIds(labIds1);


        List<ExperimentCourse> experimentCourses = experimentCourseMapper.selectExperimentCourseList(experimentCourse);
        // 设置实验室名称
        List<Long> labIds = experimentCourses.stream().map(ExperimentCourse::getLabId).collect(Collectors.toList());
        Lab lab = new Lab();
        lab.setLabIds(labIds);
        List<Lab> labs = labMapper.selectLabList(lab);
        // 转为map
        Map<Long,Lab> labMap = labs.stream().collect(Collectors.toMap(Lab::getId, Function.identity()));
        experimentCourses.forEach(x->{
            x.setLabName(labMap.get(x.getLabId()).getLabName());
            x.setLocation(labMap.get(x.getLabId()).getLocation());
            // 获取实验剩余容纳人数
            x.setRemainCapacity(getRemainCapacity(x));
        });
        return experimentCourses;
    }

    public Integer getRemainCapacity(ExperimentCourse experimentCourse){
        // 获取预约记录
        Reservations reservations = new Reservations();
        reservations.setCourseId(experimentCourse.getId());
        List<Reservations> reservationsList = reservationsMapper.selectReservationsList(reservations);
        // 报名人数
        int count = reservationsList.size();
        // 计算剩余容量
        return experimentCourse.getMaxExperimenters().intValue() - count;
    }

    /**
     * 新增实验课程
     *
     * @param experimentCourse 实验课程
     * @return 结果
     */
    @Override
    public int insertExperimentCourse(ExperimentCourse experimentCourse) {
            return experimentCourseMapper.insertExperimentCourse(experimentCourse);
    }

    /**
     * 修改实验课程
     *
     * @param experimentCourse 实验课程
     * @return 结果
     */
    @Override
    public int updateExperimentCourse(ExperimentCourse experimentCourse) {
        return experimentCourseMapper.updateExperimentCourse(experimentCourse);
    }

    /**
     * 批量删除实验课程
     *
     * @param ids 需要删除的实验课程主键
     * @return 结果
     */
    @Override
    public int deleteExperimentCourseByIds(Long[] ids) {
        return experimentCourseMapper.deleteExperimentCourseByIds(ids);
    }

    /**
     * 删除实验课程信息
     *
     * @param id 实验课程主键
     * @return 结果
     */
    @Override
    public int deleteExperimentCourseById(Long id) {
        return experimentCourseMapper.deleteExperimentCourseById(id);
    }
}
